package com.hzit.sb2.util.weixin;

import com.alibaba.fastjson.JSONObject;
import com.hzit.sb2.bean.UserItem;
import com.hzit.sb2.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class GetUserInfoUtil {
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    HttpClientUtil httpClientUtil;
    @Autowired
    AccessTokenUtil accessTokenUtil;

    /**
     * 获取关注的用户的OpenId
     * @return list
     */
    public Map<String, Object> getUserOpenId(String nextId) {
        //http请求方式: GET（请使用https协议）
        //https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
        String accessToken = accessTokenUtil.getAccessToken();
        String url ="";
        if(nextId!=null){
            url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken+"&next_openid="+nextId;
        }
        url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken;
        String content = httpClientUtil.get(url);
        Map<String, Object> hashMap = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(content);
        hashMap = (Map<String, Object>) JSONObject.toJavaObject(jsonObject,Map.class);
        return hashMap;
    }

    /**
     * 获取单个用户信息
     * @return
     * https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
     */
    public Map<String , Object> userInfo(String openid, String lang){
        String accessToken = accessTokenUtil.getAccessToken();
        String url="";
        if(lang!=null){
            url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openid+"&lang="+lang;
        }
        url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openid+"&lang=zh_CN";
        String content = httpClientUtil.get(url);
        Map<String, Object> hashMap = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(content);
        hashMap = (Map<String, Object>) JSONObject.toJavaObject(jsonObject,Map.class);
        return hashMap;
    }

    /**
     * 获取用户列表信息
     * @param nextId
     * @param lang
     * @return
     */
    public Map<String , List<UserItem>> userInfoList(String nextId,String lang){
        String accessToken = accessTokenUtil.getAccessToken();
        String url="";
        if(lang==null){
            lang = "zh_CN";
        }
        url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="+accessToken+"&lang="+lang;
        String requestBody = requestParm(nextId,lang);
        String result = httpClientUtil.post(url, requestBody);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Map<String, List<UserItem>> map = JSONObject.toJavaObject(jsonObject, Map.class);
        return map;
    }

    /**
     * 封装requestBody
     * @param nextId
     * @return
     */
    private String requestParm(String nextId,String lang) {
        Map<String, Object> map = this.getUserOpenId(nextId);
        Map<String,Object> idsMap = (Map<String,Object>)map.get("data");
        List<String> openids = (List<String>)idsMap.get("openid");
        Map<String,Object> paramMap = new HashMap<>();
        List<Map<String,String >> list = new ArrayList<>();
        for (String openid : openids) {
            HashMap<String, String> openIdMap = new HashMap<>();
            openIdMap.put("openid",openid);
            openIdMap.put("lang",lang);
            list.add(openIdMap);
        }
        paramMap.put("user_list", list);
        String requestBody = JSONObject.toJSONString(paramMap);
        return requestBody;
    }

    //获取二维码
    public String getQrCode() {
        String accessToken = accessTokenUtil.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken;
        String requestBody = "{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}";
        String result = httpClientUtil.post(url, requestBody);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Map<String,String> map = JSONObject.toJavaObject(jsonObject, Map.class);
        String ticket = map.get("ticket");
        String imgUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
        return imgUrl;
    }
}
